﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Rison.DBHelper;
using Rison.BusinessFacade.Common;
using Rison.DataAccess.Data.NRBZ;
using Rison.DataAccess.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Entity.NRBZ;

namespace Rison.BusinessFacade.Facade.NRBZ
{
    public sealed class SysAttachmentFacade : FacadeBase
    {
        private string _tablename = string.Empty;
        private string _parentKeyname = string.Empty;
        private DbBaseHelper _dbTransaction = null;

        public SysAttachmentFacade()
        {
        }

        public SysAttachmentFacade(string tablename)
        {
            _tablename = tablename;
        }

        public SysAttachmentFacade(string tablename, DbBaseHelper dbTransaction)
        {
            _tablename = tablename;
            _dbTransaction = dbTransaction;
        }

        public SysAttachmentFacade(string tablename, string parentKeyname)
        {
            _tablename = tablename;
            _parentKeyname = parentKeyname;
        }

        public SysAttachmentFacade(string tablename, string parentKeyname, DbBaseHelper dbTransaction)
        {
            _tablename = tablename;
            _parentKeyname = parentKeyname;
            _dbTransaction = dbTransaction;
        }


        protected override DataAccessBase dbObject
        {
            get
            {
                if (dbHelper == null)
                    dbHelper = new SysAttachmentDb(_tablename, _parentKeyname, _dbTransaction);

                return dbHelper;
            }
        }

        protected override string identityFieldName
        {
            get { return "FileId"; }
        }


        protected override List<SqlField> GetSqlFieldCollection(object value, bool getIdentityField)
        {
            List<SqlField> fields = new List<SqlField>();
            if (getIdentityField)
                fields.Add(new SqlField("FileId", (value as SysAttachment).FileId, true, false));
            fields.Add(new SqlField("ParentId", (value as SysAttachment).ParentId));
            fields.Add(new SqlField("FileName", (value as SysAttachment).FileName));
            fields.Add(new SqlField("FilePath", (value as SysAttachment).FilePath));
            fields.Add(new SqlField("FileSize", (value as SysAttachment).FileSize));
            fields.Add(new SqlField("Extension", (value as SysAttachment).Extension));
            fields.Add(new SqlField("ContentType", (value as SysAttachment).ContentType));

            return fields;
        }

        protected override object SetPropertiesValue(DataRow row)
        {
            SysAttachment obj = new SysAttachment();
            obj.FileId = Convert.ToInt64(row["FileId"]);
            obj.ParentId = Convert.ToInt64(row["ParentId"]);
            obj.FileName = row["FileName"].ToString();
            obj.FilePath = StringHelper.GetValue(row, "FilePath");
            obj.FileSize = StringHelper.GetValue(row, "FileSize");
            obj.Extension = StringHelper.GetValue(row, "Extension");
            obj.ContentType = StringHelper.GetValue(row, "ContentType");

            return obj;
        }


        public int SaveAttachment(List<SysAttachment> atts)
        {
            return SaveAttachment(atts, 0);
        }

        public int SaveAttachment(List<SysAttachment> atts, long parentId)
        {
            if (atts == null || atts.Count <= 0) return 0;

            dbObject.BeginTransaction();

            try
            {
                if ((dbObject as SysAttachmentDb).DeleteAttachmentByParent(parentId) >= 0)
                {
                    foreach (SysAttachment a in atts)
                    {
                        if (a.ParentId != parentId && parentId > 0) a.ParentId = parentId;

                        base.Save(a);
                    }

                    dbObject.CommitTransaction();
                }

                return atts.Count;
            }
            catch
            {
                dbObject.RollBackTransaction();
            }

            return -1;
        }
    }
}
